/* js for register page */

(function() {
    window.onload = function() {
      var WAITING = 2000;
      var  queryCounter = 0;
      var map, selfMarker, options, center, lastZoomLevel;
      var homeLatitude, homeLongitude, homeLatLng;
      var lastLatitude, lastLongitude, lastLatLng;

      function autoFill(results) {
        $('#register-geocode-query').css({'color':'#1a1a1a'});
        document.getElementById('register-geocode-query').value = results[0].formatted_address;
      }

      function moveMarker(results) {
        if (!results[0]) {
          document.getElementById('register-geocode-query').value = '请再试一次';
        } else {
          var location = results[0].geometry.location;

          selfMarker.setPosition(location);
          map.setCenter(location);
          utils.writeLatLng(location.Ia, location.Ja);
        }
      }

      homeLatitude = utils.readCookie('homeLatitude');
      homeLongitude = utils.readCookie('homeLongitude');
      homeLatLng = new google.maps.LatLng(homeLatitude, homeLongitude);

      //lastLatitude = utils.readCookie('lastLatitude') || homeLatitude;
      //lastLongitude = utils.readCookie('lastLongitude') || homeLongitude;
      //lastLatLng = new google.maps.LatLng(lastLatitude, lastLongitude);

      center = homeLatLng;
      lastZoomLevel = parseInt(utils.readCookie('lastZoomLevel'), 10) || 10;

      options = {
        center: center,
        zoom: lastZoomLevel,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        disableDefaultUI: true,
        navigationControl: true,
        navigationControlOptions: {
          position: google.maps.ControlPosition.RIGHT_BOTTOM,
          style: google.maps.NavigationControlStyle.SMALL
        }
      };

      map = new google.maps.Map(document.getElementById('map-register'), options);

      var control = document.createElement('div');
      //control.id = 'button-home';
      //control.style.position = 'absolute';
      //control.style.top = '10px';
      //control.style.right = '10px';
      control.innerHTML = '<img src="/static/img/map/home.png" width="50" height="50" />';
      //control.index = 1;

      // Add the control to the map
      map.controls[google.maps.ControlPosition.TOP_RIGHT].push(control);

      // When the button is clicked pan to sydney
      google.maps.event.addDomListener(control, 'click', function() {
          console.log('back to home');
          map.setCenter(homeLatLng);
          selfMarker.setPosition(homeLatLng);
          lastLatitude = homeLatitude;
          lastLongitude = homeLongitude;
          utils.createCookie('lastLatitude', lastLatitude);
          utils.createCookie('lastLongitude', lastLongitude);    
      });

      // Change the style when the user mousedowns
      //google.maps.event.addDomListener(control, 'mousedown', function() {
      //    control.className = 'button-home';
      //});

      // Change the style back when the user mouseups
      //google.maps.event.addDomListener(control, 'mouseup', function() {
      //    control.className = 'button-home';
      //});

      google.maps.event.addListener(map, 'zoom_changed', function() {
          var zoomLevel = map.getZoom();
          utils.createCookie('lastZoomLevel', zoomLevel, 365);
          console.log('lastZoomLevel: ' + zoomLevel);
      });

      selfMarker = gmap.createSelfMarker(map, lastLatLng || homeLatLng);                             
      utils.writeLatLng(lastLatitude || homeLatitude, lastLongitude || homeLongitude);

      console.log('map has been created!');
      google.maps.event.addListener(selfMarker, 'dragend', function() {
          selfMarker.setOptions({icon: gmap.selfMarker});
          var lastLatLng = selfMarker.getPosition();
          var expires = new Date(Date.now() + gmap.ONE_DAY * 365).toUTCString();
          lastLatitude = lastLatLng.lat();
          lastLongitude = lastLatLng.lng();
          console.log('lastLatidue: ' + lastLatitude);
          console.log('lastLongitude: ' + lastLongitude);
          console.log('start set cookie');
          utils.createCookie('lastLatitude', lastLatitude);
          utils.createCookie('lastLongitude', lastLongitude);
          console.log(lastLatitude);
          console.log(lastLongitude);
          console.log('end set cookie');
          map.setCenter(lastLatLng);

          utils.writeLatLng(lastLatitude, lastLongitude);

          // If the 'from location to addrss' radio button is selected,
          // then call geocode
          if ($('input#from-location-to-address:checked').val()) {
            gmap.geocode({'latLng':lastLatLng}, autoFill);
          }                                                          
      });                                                                               

      $('input#register-geocode-query').change(function() {
          var address = $(this).val();
          if ($('input#from-address-to-location').val()) {
            gmap.geocode({'address':address}, moveMarker);
          }
      });

      $('input#register-geocode-query').click(function() {
            $(this).val('');
            $(this).css({'color':'#1a1a1a'});

            $(this).unbind('click');
      });

      $('input#id_username').keyup(function() {
          popup.create();

          var input = this,
              username = $(input).val();

          if (this.timer) { clearTimeout(this.timer); }

          this.timer = setTimeout(function() {
              $.ajax({
                  url:'/account/validate/',
                  data:'username='+username,
                  dataType: 'json',
                  type: 'get',
                  error: function() {
                    popup.remove('服务器开小差了');
                  },
                  success: function (results) {
                    if (!results.ok) {
                      popup.remove('出了一些问题，请稍候再试');

                    } else if (results.ok && !results.exits) {
                      popup.remove('用户名可用');
                    } else { // Failed
                      popup.remove('用户名已经被注册了');
                      $(input).val('');
                    }
                  }
              });
            },
            WAITING);
        });

        $('input#id_username').click(function() {
            $(this).val('');
            $(this).css({'color':'#1a1a1a'});


            $(this).unbind('click'); 











        });

        $('input#id_password').click(function() {
            $(this).val('');
            $(this).css({'color':'#1a1a1a'});

            $(this).unbind('click'); 
        });

        $('input#id_email').click(function() {
            $(this).val('');
            $(this).css({'color':'#1a1a1a'});

            $(this).unbind('click'); 



























        });                               

      };
  })();
